package cn.mes.module.mes.controller.admin.baseworkshop;

import cn.mes.module.mes.controller.admin.basemachineinfo.vo.BaseMachineInfoPageReqVO;
import cn.mes.module.mes.enums.MesCommonStatus;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.CommonResult;
import cn.mes.framework.common.util.object.BeanUtils;
import static cn.mes.framework.common.pojo.CommonResult.success;

import cn.mes.framework.excel.core.util.ExcelUtils;

import cn.mes.framework.apilog.core.annotation.ApiAccessLog;
import static cn.mes.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.mes.module.mes.controller.admin.baseworkshop.vo.*;
import cn.mes.module.mes.dal.dataobject.baseworkshop.BaseWorkShopDO;
import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseMachineInfoDO;
import cn.mes.module.mes.dal.dataobject.baseworkshop.BaseMachineAreaDO;
import cn.mes.module.mes.service.baseworkshop.BaseWorkShopService;

@Tag(name = "管理后台 - 车间信息")
@RestController
@RequestMapping("/mes/base-work-shop")
@Validated
public class BaseWorkShopController {

    @Resource
    private BaseWorkShopService baseWorkShopService;

    @PostMapping("/create")
    @Operation(summary = "创建车间信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:create')")
    public CommonResult<String> createBaseWorkShop(@Valid @RequestBody BaseWorkShopSaveReqVO createReqVO) {
        return success(baseWorkShopService.createBaseWorkShop(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车间信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:update')")
    public CommonResult<Boolean> updateBaseWorkShop(@Valid @RequestBody BaseWorkShopSaveReqVO updateReqVO) {
        baseWorkShopService.updateBaseWorkShop(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车间信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:delete')")
    public CommonResult<Boolean> deleteBaseWorkShop(@RequestParam("id") String id) {
        baseWorkShopService.deleteBaseWorkShop(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车间信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:query')")
    public CommonResult<BaseWorkShopRespVO> getBaseWorkShop(@RequestParam("id") String id) {
        BaseWorkShopDO baseWorkShop = baseWorkShopService.getBaseWorkShop(id);
        return success(BeanUtils.toBean(baseWorkShop, BaseWorkShopRespVO.class));
    }

    @GetMapping({"/list-all-simple", "simple-list-work-shop"})
    @Operation(summary = "获取车间精简信息列表", description = "只包含被开启的车间，用于【角色分配车间】功能的选项。" +
            "在多租户的场景下，会只返回租户所在套餐有的车间")
    public CommonResult<List<BaseWorkShopRespVO>> getSimpleWorkShopList() {
        List<BaseWorkShopDO> list = baseWorkShopService.getWorkShopList(new WorkShopListReqVO().setStatus(MesCommonStatus.ENABLE.getCode()));
        list.sort(Comparator.comparing(BaseWorkShopDO::getOrderNo));
        return success(BeanUtils.toBean(list, BaseWorkShopRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车间信息分页")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:query')")
    public CommonResult<PageResult<BaseWorkShopDO>> getBaseWorkShopPage(@Valid BaseWorkShopPageReqVO pageReqVO) {
        PageResult<BaseWorkShopDO> pageResult = baseWorkShopService.getBaseWorkShopPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车间信息 Excel")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseWorkShopExcel(@Valid BaseWorkShopPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseWorkShopDO> list = baseWorkShopService.getBaseWorkShopPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车间信息.xls", "数据", BaseWorkShopRespVO.class,
                        BeanUtils.toBean(list, BaseWorkShopRespVO.class));
    }

    // ==================== 子表（生产区域信息） ====================

    @GetMapping("/base-machine-area/page")
    @Operation(summary = "获得生产区域信息分页")
    @Parameter(name = "workShopId", description = "所属车间ID")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:query')")
    public CommonResult<PageResult<BaseMachineAreaDO>> getBaseMachineAreaPage(PageParam pageReqVO,
                                                                              @RequestParam("workShopId") String workShopId) {
        return success(baseWorkShopService.getBaseMachineAreaPage(pageReqVO, workShopId));
    }

    @GetMapping({"/base-machine-area/list-all-simple", "/base-machine-area/simple-list-machine-area"})
    @Operation(summary = "获取车间精简信息列表", description = "只包含被开启的生产区域，用于【角色分配车间】功能的选项。" +
            "在多租户的场景下，会只返回租户所在套餐有的生产区域")
    public CommonResult<List<BaseMachineAreaDO>> getSimpleMachineAreaList() {
        List<BaseMachineAreaDO> list = baseWorkShopService.getMachineAreaList(new MachineAreaListReqVO().setStatus(MesCommonStatus.ENABLE.getCode()));
        list.sort(Comparator.comparing(BaseMachineAreaDO::getOrderNo));
        return success(BeanUtils.toBean(list, BaseMachineAreaDO.class));
    }

    @PostMapping("/base-machine-area/create")
    @Operation(summary = "创建生产区域信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:create')")
    public CommonResult<String> createBaseMachineArea(@Valid @RequestBody BaseMachineAreaDO baseMachineArea) {
        return success(baseWorkShopService.createBaseMachineArea(baseMachineArea));
    }

    @PutMapping("/base-machine-area/update")
    @Operation(summary = "更新生产区域信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:update')")
    public CommonResult<Boolean> updateBaseMachineArea(@Valid @RequestBody BaseMachineAreaDO baseMachineArea) {
        baseWorkShopService.updateBaseMachineArea(baseMachineArea);
        return success(true);
    }

    @DeleteMapping("/base-machine-area/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除生产区域信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:delete')")
    public CommonResult<Boolean> deleteBaseMachineArea(@RequestParam("id") String id) {
        baseWorkShopService.deleteBaseMachineArea(id);
        return success(true);
    }

    @GetMapping("/base-machine-area/get")
    @Operation(summary = "获得生产区域信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:query')")
    public CommonResult<BaseMachineAreaDO> getBaseMachineArea(@RequestParam("id") String id) {
        return success(baseWorkShopService.getBaseMachineArea(id));
    }

    // ==================== 子表（机台产线基础信息） ====================

    @GetMapping("/base-machine-info/page")
    @Operation(summary = "获得机台产线基础信息分页")
    @Parameter(name = "workShopId", description = "车间ID")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:query')")
    public CommonResult<PageResult<BaseMachineInfoDO>> getBaseMachineInfoPage(@Valid BaseMachineInfoPageReqVO pageReqVO) {
        return success(baseWorkShopService.getBaseMachineInfoPage(pageReqVO));
    }

    @PostMapping("/base-machine-info/create")
    @Operation(summary = "创建机台产线基础信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:create')")
    public CommonResult<String> createBaseMachineInfo(@Valid @RequestBody BaseMachineInfoDO baseMachineInfo) {
        return success(baseWorkShopService.createBaseMachineInfo(baseMachineInfo));
    }

    @PutMapping("/base-machine-info/update")
    @Operation(summary = "更新机台产线基础信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:update')")
    public CommonResult<Boolean> updateBaseMachineInfo(@Valid @RequestBody BaseMachineInfoDO baseMachineInfo) {
        baseWorkShopService.updateBaseMachineInfo(baseMachineInfo);
        return success(true);
    }

    @DeleteMapping("/base-machine-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除机台产线基础信息")
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:delete')")
    public CommonResult<Boolean> deleteBaseMachineInfo(@RequestParam("id") String id) {
        baseWorkShopService.deleteBaseMachineInfo(id);
        return success(true);
    }

	@GetMapping("/base-machine-info/get")
	@Operation(summary = "获得机台产线基础信息")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-work-shop:query')")
	public CommonResult<BaseMachineInfoDO> getBaseMachineInfo(@RequestParam("id") String id) {
	    return success(baseWorkShopService.getBaseMachineInfo(id));
	}
}